﻿using AsmShaderConverter.AsmShader;
using AsmShaderConverter.HlslShader;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AsmShaderConverter.AsmFunctions
{
    public class AsmFunctionMad : IConvertToHlsl
    {
        readonly string originalLineOfCode;
        private Register destination;
        private Register source1;
        private Register source2;
        private Register source3;
        public Register GetDestinationRegister()
        {
            return this.destination;
        }
        public AsmFunctionMad(string lineOfCode)
            : this(lineOfCode, false)
        {

        }
        public AsmFunctionMad(string lineOfCode, bool halfPrecision)
        {
            this.originalLineOfCode = lineOfCode;

            lineOfCode = lineOfCode.Replace(",", "");
            lineOfCode = lineOfCode.Trim();
            var values = lineOfCode.Split(' ');

            switch (values.Length)
            {
                case 5:
                    this.destination = new Register(values[1], true, Swizzle.StandardXYZW);
                    this.source1 = new Register(values[2], destination.swizzle);
                    this.source2 = new Register(values[3], destination.swizzle);
                    this.source3 = new Register(values[4], destination.swizzle);
                    break;
                default:
                    throw new Exception("Function can´t be converted! " + lineOfCode);
            }

        }

        public string ToHLSL(AsmShaderFile asmShader)
        {

            if (destination.swizzle.Length < source1.swizzle.Length || source1.swizzle.Length == 0)
            {
                string swizzle = new string(source1.swizzle).Substring(0, destination.swizzle.Length);
                source1.swizzle = swizzle.ToCharArray();
            }

            if (destination.swizzle.Length < source2.swizzle.Length || source2.swizzle.Length == 0)
            {
                string swizzle = new string(source2.swizzle).Substring(0, destination.swizzle.Length);
                source2.swizzle = swizzle.ToCharArray();
            }

            if (destination.swizzle.Length < source3.swizzle.Length || source3.swizzle.Length == 0)
            {
                string swizzle = new string(source3.swizzle).Substring(0, destination.swizzle.Length);
                source3.swizzle = swizzle.ToCharArray();
            }

            //if (source1.swizzle.Length == 0)
            //    if (destination.swizzle != source1.swizzle)
            //    {
            //        source1.swizzle = destination.swizzle;
            //    }

            return String.Format("{0} = {1} * {2} +  {3};", this.destination.ToHLSL(asmShader), this.source1.ToHLSL(asmShader), this.source2.ToHLSL(asmShader), this.source3.ToHLSL(asmShader));
        }

        public string ToHLSLWitchComment(AsmShaderFile asmShader)
        {
            return this.ToHLSL(asmShader) + "//" + this.originalLineOfCode;
        }
    }
}
